package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Dept1;
import com.woniu.pojo.Dept;
import com.woniu.pojo.Emp;
import com.woniu.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("dept")
public class DeptController {
    @Autowired
    DeptService deptService;

    /**
     * 跳转院系list页面
     * */
    @RequestMapping("formList")
    public String formList(){
        System.out.println("DeptController.formList");
        return "WEB-INF/dept/deptList";
    }

    /**
     * 跳转到修改页面
     * */
    @RequestMapping("formUpdate")
    public String updateIndex(Integer id, Model model){
        model.addAttribute("id",id);
        return "WEB-INF/dept/update";
    }

    /**
     * 跳转到增加页面
     * */
    @RequestMapping("formAdd")
    public String formAdd(){
        return "WEB-INF/dept/add";
    }

    /**
     * 查找主管级别的员工
     * */
    @RequestMapping("findZg")
    @ResponseBody
    public List<Emp> findZg(){
        List<Emp> zg = deptService.findZg();
        return zg;
    }

    /**
     * 增加
     **/
    @RequestMapping("add")
    @ResponseBody
    public String add(Dept dept){
        try {
            deptService.add(dept);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return "ok";
    }

   /**
    * 删除
    * */
    @RequestMapping("deleteById")
    @ResponseBody
    public String deleteById(Integer deptId){
        try {
           deptService.deleteById(deptId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return "ok";
    }

   /**
    * 批量删除
    * */
    @RequestMapping("deleteByIds")
    @ResponseBody
    public String deleteByIds(Integer[] deptId){

        for (Integer e : deptId) {
            System.out.println(e);
        };
        try {
           deptService.deleteByIds(Arrays.asList(deptId));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    return  "ok";
    }



    /**
     * 查一条
     **/
    @RequestMapping("findOne")
    @ResponseBody
    public Map<String,Object> findOne(Integer id){
        Map<String,Object> map = new HashMap<>();
        try {
          Dept1 dept = deptService.findOne(id);
          map.put("dept",dept);
          map.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("result",false);
        }
        return map;
    }

    /**
     *  修改
     **/
    @RequestMapping("updateById")
    @ResponseBody
    public String updateById(Dept dept){
        try {
           deptService.updateById(dept);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return "ok";
    }


    /**
     * 查询，按条件
     **/
    @RequestMapping("findByExample")
    @ResponseBody
    public Map<String,Object> findByExample(Dept dept, Integer page,Integer limit){
        Map<String,Object> map = new HashMap<>();

        PageHelper.startPage(page,limit);
        List<Dept1> deptList = deptService.findByExample(dept);
        PageInfo<Dept1> pageInfo = new PageInfo<>(deptList);

        map.put("data", pageInfo.getList());
        map.put("msg", "");
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        return map;
    }

    /**
     * 查询全部
     **/
    @RequestMapping("findAll")
    @ResponseBody
    public List findAll(){
        List<Dept1> deptList = null;
        try {
            deptList = deptService.findByExample(new Dept());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return deptList;
    }

}
